//index.js
//获取应用实例
var app = getApp();
//console.log(app.globalData);
Page({
  data: {
    userInfo: {},
    region: [],
    customItem: '全部',
    status: 0,
    location: '',
    address: '',
    id: 0,
    store_id: 0,
    data: {},
    release_day:'',
    deadline:'',
    out_room_id:''
  },
  //设置时间
  bindDateChange: function (e) {
    var v = e.detail.value;
    var name = e.currentTarget.dataset.name;
    if (name == "release_day") {
      this.setData({ release_day: v });
    } else {
      this.setData({ deadline: v });
    }
   // this.datedifference();

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id ) {
      this.data.id = options.id ;
    
      this.init();
    }
    if (options.out_room_id) {
      this.setData({ out_room_id: options.out_room_id })
    }

   
  },
  // onShow: function () {
  //   var id = app.getPageData("index/issue", "id");
  //   if (id) {
  //     this.data.id = id;
  //     this.init();
  //     app.setPageData("index/issue", { id: 0 });
  //   }
  // },
  init: function () {
    var that = this;
    app.api("api/community_bills/find", {
      bill_entry_id: this.data.id,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      //var poi = res.data.community_locations.split("|")
      that.setData({
        data: res.data,
        deadline: res.data.deadline,
        release_day: res.data.release_day,
      });
    })
  },

  formSubmit: function (e) {
    var postData = e.detail.value;
    if (postData.out_room_id == "") {
      app.alert("请输小区房屋唯一编号");
      return false;
    }
    if (postData.cost_type == "") {
      app.alert("请输入费用类型");
      return false;
    }
    if (postData.bill_entry_amount == "") {
      app.alert("请输入应收金额");
      return false;
    }
    if (this.data.release_day == "") {
      app.alert("请选择账期");
      return false;
    }

    if (this.data.deadline == "") {
      app.alert("请选择缴费截止日期");
      return false;
    }
    if (this.data.acct_period == "") {
      app.alert("请输入账期");
      return false;
    }
    
    var that = this;
    that.setData({ 'loading': 1 });
    that.setData({ 'disabled': 1 });
    app.api("api/community_bills/add", {
      method: 'POST',
      bill_entry_id: this.data.id ? this.data.id:'',
      out_room_id:  postData.out_room_id,
      cost_type: postData.cost_type,
      acct_period: postData.acct_period,
      bill_entry_amount: postData.bill_entry_amount,
      release_day: this.data.release_day,
      deadline: this.data.deadline,
      remark_str: postData.remark_str,
      //out_room_id: postData.out_room_id,
      
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0 });
        that.setData({ 'disabled': 0 });
        return false;
      }
     // that.reset();
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000
      })
      app.setPageData("tenement/bills", { reload: 1 });
      wx.navigateBack();
    })

  }


})
